
<template>
  <div class="drawerBox">
    <el-drawer v-model="drawerData.open" v-if="drawerData.open">
      <template #header>
        <div style="width: 100%">{{ drawerData.title }}</div>
      </template>
      <template #default>
        <el-form
          :model="form"
          label-width="120px"
          :rules="formRules"
          ref="forms"
          class="formBox"
        >
          <el-form-item label="题目：" prop="title">
            <el-input
              v-model="form.title"
              placeholder="请输入题目"
              allow-caear
            />
          </el-form-item>
          <el-form-item label="图片：" class="typeUpload" prop="filePath">
            <div class="imgmin">
              <el-upload
                class="avatar-uploader"
                :action="uploadAction"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img
                  v-if="form.filePath"
                  :src="url.baseUrl + 'uploadPath/question/' + form.filePath"
                  class="avatar"
                />
                <el-icon v-else class="avatar-uploader-icon">
                  <Plus />
                </el-icon>
              </el-upload>
            </div>
            <div class="imgtext">图片大小需小于2MB，格式为jpg、png</div>
          </el-form-item>
          <el-form-item label="描述：" prop="content">
            <el-input
              v-model="form.content"
              type="textarea"
              placeholder="请输入"
            />
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <div style="flex: auto">
          <el-button
            class="secondBtn"
            @click="resetForm(forms), (drawerData.open = false)"
            >返回</el-button
          >
          <el-button class="mainBtn" @click="submitForm(forms)">保存</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>


<script  setup>
import { ref, watch, defineEmits, inject } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { questionsAdd, questionsEdit } from "../../../api/index";
const url = inject("baseUrl");
const emit = defineEmits(["getLists"]);

const drawerData = ref({
  open: false,
  title: "",
});

//
// 表单
//

//校验规则
const formRules = ref({
  title: [
    {
      required: true,
      message: "请输入题目",
      trigger: "blur",
    },
  ],

  content: [
    { required: true, message: "请输入描述", trigger: "blur" },
    {
      min: 0,
      max: 200,
      message: "描述长度不能超过200个字符",
      trigger: "blur",
    },
  ],
  filePath: [{ required: true, message: "请上传照片", trigger: "change" }],
});
const forms = ref(null);
let form = ref({
  id: "",
  title: "",
  content: "",
  fileName: "",
  filePath: "",
});

watch(
  drawerData.value,
  (newValue, oldValue) => {
    if (drawerData.value.open == true && forms.value) {
      resetForm(forms.value);
    }
  },
  { deep: true, immediate: true }
);

// 图片
const uploadAction = ref(url.baseUrl + "question/uploadTitlePicture");

const handleAvatarSuccess = (response, uploadFile) => {
  console.log(response);
  form.value.fileName = response.name;
  form.value.filePath = response.name;
};
const beforeAvatarUpload = (rawFile) => {
  if (rawFile.type !== "image/jpeg" && rawFile.type !== "image/png") {
    ElMessage.error("支持jpeg，png!");
    return false;
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error("最大 2MB!");
    return false;
  }

  return true;
};

//表单保存
const submitForm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      // console.log("submit!");
      if (drawerData.value.title == "新增试题") {
        questionsAdd(form.value).then((res) => {
          if (res.code == 200) {
            emit("getLists", res);
            ElMessage.success("保存成功");
            drawerData.value.open = false;
          }
        });
      } else {
        questionsEdit(form.value).then((res) => {
          if (res.code == 200) {
            emit("getLists", res);
            ElMessage.success("保存成功");
            drawerData.value.open = false;
          }
        });
      }
    } else {
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};

defineExpose({ drawerData, form });
</script>


<style lang="scss" scoped>
.drawerBox {
  :deep(.el-drawer .el-drawer__body) {
    padding: 16px;
    .el-form-item {
      .el-form-item__label {
        font-size: 14px;
        font-family: MicrosoftYaHei;
        color: #999999;
        padding: 0px;
        margin-right: 8px;
      }
      .el-form-item__content {
        .el-input {
          width: 280px;
        }
      }
    }
  }
}

// 上传
.typeUpload :deep(.el-form-item__content) {
  flex-wrap: inherit !important;
  align-items: flex-end !important;

  div:first-child {
    margin-right: 8px;

    .el-upload--picture-card {
      background-color: #ffffff;
    }
  }

  .imgtext {
    font-size: 14px;
    font-family: MicrosoftYaHei;
    color: #999999;
    line-height: 24px;
  }
  .avatar {
    width: 146px;
    height: 146px;
    border: 1px dashed transparent;
  }

  .el-upload {
    border: 1px dashed #ebebeb;
    border-radius: 6px;
    width: 146px;
    height: 146px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

// 单选
:deep(.el-radio-group) {
  .el-radio__label {
    font-size: 14px;
    font-family: MicrosoftYaHei;
    color: #333333 !important;
  }
  .el-radio__input.is-checked .el-radio__inner {
    border-color: #3370ff;
    background: #3370ff;
  }
}

// 多选
:deep(.el-checkbox-group) {
  .el-checkbox__input.is-checked .el-checkbox__inner {
    border-color: #3370ff;
    background: #3370ff;
  }
  .el-checkbox__label {
    font-size: 14px;
    font-family: MicrosoftYaHei;
    color: #333333 !important;
  }
}
</style>



